import {GetMenuData_ACTION, ChangeOpenKey_ACTION, SiderCollapsed_ACTION} from "../actionType";
import {AddTabsPane_ACTION, ChangeTabsData_ACTION} from "../actionType";
import Immutable from "immutable";

let menuDefaultState = {
    /**
     * 首页menu数据
     */
    menuList: [],
    /**
     * menu展开的key
     */
    openKey: [],
    /**
     * sider的展开回收
     */
    collapsed: false,
};
export const menuData = (state = menuDefaultState, action) => {
    switch (action.type) {
        case GetMenuData_ACTION:
            return {...state, ...action}
        case ChangeOpenKey_ACTION:
            return {...state, ...action}
        case SiderCollapsed_ACTION:
            return {...state, collapsed: !state.collapsed}
         default:
            return state
    }
};

export const tabsData = (state = {tabsList: [],activeKey: ""}, action) => {
    switch (action.type) {
        case AddTabsPane_ACTION:
            //避免引用类型数据，使用immutable进行数据转换
            let imuTabsList = Immutable.List(state.tabsList);
            let flag = false;
            for (let i = 0; i < state.tabsList.length; i++) {
                if (state.tabsList[i].key === action.key) {
                    flag = true;
                    break;
                }
            }
            imuTabsList = flag ? imuTabsList : imuTabsList.push({key: action.key, title: action.title});
            // 引用变了，state才变化
            return {...state, ...{tabsList: imuTabsList.toJS()}, activeKey: action.key}
        case ChangeTabsData_ACTION:
            return {...state, ...action}
        default:
            return state
    }
}
